package cn.jeeweb.web.modules.sys.service.impl;

import cn.jeeweb.common.mybatis.mvc.service.impl.CommonServiceImpl;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.modules.sys.entity.HomeRoleMenu;
import cn.jeeweb.web.modules.sys.entity.RoleMenu;
import cn.jeeweb.web.modules.sys.mapper.HomeRoleMenuMapper;
import cn.jeeweb.web.modules.sys.service.IHomeRoleMenuService;
import cn.jeeweb.web.modules.sys.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service("homeRoleMenuService")
public class HomeRoleMenuServiceImpl extends CommonServiceImpl<HomeRoleMenuMapper,HomeRoleMenu> implements IHomeRoleMenuService {

    @Override
    public void setHomeRoleMenu(String roleId, String allMenuIds) {
        if (!StringUtils.isEmpty(allMenuIds)) {
            // 删除菜单关联
            delete(new EntityWrapper<HomeRoleMenu>(HomeRoleMenu.class).eq("roleId", roleId));
            String[] selectMenus = allMenuIds.split(",");
            List<HomeRoleMenu> roleMenuList = new ArrayList<HomeRoleMenu>();
            for (String menuId : selectMenus) {
                HomeRoleMenu roleMenu = new HomeRoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenuList.add(roleMenu);
            }
            insertOrUpdateBatch(roleMenuList);
        }
    }
}
